package com.cmict.data.infra.dal.mysql.source;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.infra.dal.dataobject.source.SourceDO;
import org.apache.ibatis.annotations.Mapper;
import com.cmict.data.infra.controller.admin.source.vo.*;

/**
 * 数据源 Mapper
 *
 * @author awesome1114
 */
@Mapper
public interface SourceMapper extends BaseMapperX<SourceDO> {

    default PageResult<SourceDO> selectPage(SourcePageReqVO reqVO) {
        QueryWrapper<SourceDO> queryWrapper=new QueryWrapper<>();
        if(reqVO.getKeyword()!=null){
            queryWrapper.lambda().and(qw -> qw.like(SourceDO::getCode, reqVO.getKeyword())
                    .or().like(SourceDO::getName, reqVO.getKeyword()));
        }
        queryWrapper.lambda().orderByDesc(SourceDO::getCreateTime);
        return selectPage(reqVO,queryWrapper);
    }
    default List<SourceDO> selectListByName(String name) {
        return selectList(new LambdaQueryWrapperX<SourceDO>()
                .eqIfPresent(SourceDO::getName, name));
    }

    default List<SourceDO> getSourceList() {
        return selectList(new LambdaQueryWrapperX<SourceDO>()
                .orderByDesc(SourceDO::getId));
    }

    default List<SourceDO> selectList(SourceExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<SourceDO>()
                .likeIfPresent(SourceDO::getName, reqVO.getName())
                .eqIfPresent(SourceDO::getCode, reqVO.getCode())
                .eqIfPresent(SourceDO::getDbId, reqVO.getDbId())
                .eqIfPresent(SourceDO::getSqlSentence, reqVO.getSqlSentence())
                .eqIfPresent(SourceDO::getDescription, reqVO.getDescription())
                .betweenIfPresent(SourceDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(SourceDO::getId));
    }

    default List<SourceDO> getListchooseByExcludeId(Long excludeId){
        return selectList(new LambdaQueryWrapperX<SourceDO>()
                .ne(excludeId != null ,SourceDO::getId,excludeId)
                .orderByDesc(SourceDO::getId));
    }
}
